import unittest
from parse.htmlchunks import htmlchunks, parsehtml, HtmlTag, HtmlText, HtmlComment

#class TestSkiptagcontent(unittest.TestCase):
#    def test001(self):
#        html = """<style>h1 {color: green}</style> <h1>HELLO!</h1>"""         
#        chunks = tuple(skiptagcontent(['style'], htmlchunks(html)))
#        control_chunks = (
#            " ",            
#            HtmlTag("<h1>"), 
#            "HELLO!",
#            HtmlTag("</h1>"), 
#        )
#        self.assertEqual(chunks, control_chunks)
    
        
class TestParseHtml(unittest.TestCase):    
    def testHtmlTag(self):
        self.assertEqual(HtmlTag("<h1>").name, 'h1')
        self.assertEqual(HtmlTag("<TITLE>").name, 'title')
        
    def testHtmlTagAttrs(self):
        self.assertEqual(HtmlTag("<div id='kk'>").attrs, {'id': 'kk'})
        self.assertEqual(HtmlTag('<div id="ll">').attrs, {'id': 'll'})
        self.assertEqual(HtmlTag('<div id=mm>').attrs, {'id': 'mm'})         
        self.assertEqual(HtmlTag('<div id=mm title=none>').attrs, {'id': 'mm', 'title': 'none'})
        self.assertEqual(HtmlTag('<div id="mm" title=none>').attrs, {'id': 'mm', 'title': 'none'})
        
    def testHtmlTagPair(self):
        self.assertTrue(HtmlTag("<div>").is_pair)
        self.assertTrue(HtmlTag("<a>").is_pair)
        self.assertFalse(HtmlTag("<img>").is_pair)
        self.assertFalse(HtmlTag("<br>").is_pair)
        self.assertFalse(HtmlTag("<tag />").is_pair)
        
    def test001(self):
        html = """<h1>HELLO!</h1> <p>It's me.</P>"""         
        chunks = tuple(htmlchunks(html))
        control_chunks = (
            HtmlTag("<h1>"), "HELLO!", HtmlTag("</h1>"),
            " ",
            HtmlTag("<p>"), "It's me.", HtmlTag("</p>"),
        )
        self.assertEqual(chunks, control_chunks)
        
    def test002(self):
        html = """<h1 id="kuku">HELLO!</h1>"""         
        chunks = tuple(htmlchunks(html))
        control_chunks = (
            HtmlTag("<h1 id='kuku'>"), "HELLO!", HtmlTag("</h1>"),
        )
        self.assertEqual(chunks, control_chunks)

    def test003(self):
        html = """text"""         
        chunks = tuple(htmlchunks(html))
        control_chunks = (
            HtmlTag('text'),
        )
        self.assertEqual(chunks, control_chunks)

    def test003(self):
        html = """ohm<br><br>padme"""         
        chunks = tuple(htmlchunks(html))
        control_chunks = (
            'ohm',
            HtmlTag('<br>'),
            HtmlTag('<br>'),
            'padme',
        )
        self.assertEqual(chunks, control_chunks)

    def testComment001(self):
        html = """A<!-- B -->C"""         
        chunks = tuple(htmlchunks(html))
        control_chunks = (
            'A',
            'C',
        )
        self.assertEqual(chunks, control_chunks)

    def testComment002(self):
        html = """A<!-- B -->C"""         
        chunks = tuple(htmlchunks(html, return_comments=True))
        control_chunks = (
            'A',
            HtmlComment(' B '),
            'C',
        )
        self.assertEqual(chunks, control_chunks)

    def testComment003(self):
        html = """t <b>1</b> A<!-- B -->C"""         
        chunks = tuple(htmlchunks(html))
        control_chunks = (
            't ',
            HtmlTag('<b>'),
            '1',
            HtmlTag('</b>'),
            ' A',
            'C',
        )
        self.assertEqual(chunks, control_chunks)

    def testCdata001(self):
        html = """z<script><![CDATA[123]]></script>x"""         
        chunks = tuple(htmlchunks(html))
        control_chunks = (
            'z',
            HtmlTag('<script>'),
            '123',
            HtmlTag('</script>'),
            'x',
        )
        self.assertEqual(chunks, control_chunks)

    def testCdata002(self):
        html = """z<script>&amp;<![CDATA[&amp;]]></script>x"""         
        chunks = tuple(htmlchunks(html))
        control_chunks = (
            'z',
            HtmlTag('<script>'),
            '&&amp;',
            HtmlTag('</script>'),
            'x',
        )
        self.assertEqual(chunks, control_chunks)

    def testEntity(self):
        html = """m&amp;m's &"""         
        chunks = tuple(htmlchunks(html))
        control_chunks = (
            "m&m's &",
        )
        self.assertEqual(chunks, control_chunks)

if __name__ == '__main__':
    unittest.main()
